package com.wlhse.service.impl;

import com.wlhse.dao.CompanyDao;
import com.wlhse.dto.outDto.CompanyOutDto;
import com.wlhse.entity.CompanyPojo;
import com.wlhse.service.CompanyService;
import com.wlhse.util.R;
import com.wlhse.util.TreeUtil;
import com.wlhse.util.state_code.CodeDict;
import com.wlhse.util.state_code.NR;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service
public class CompanyServiceImpl implements CompanyService {

    @Resource
    private CompanyDao companyDao;

    @Resource
    private TreeUtil treeUtil;

    @Override
    public R listTreeCompany() {
        R ok = R.ok();
        ok.put("data", treeUtil.getCompanyTree(companyDao.queryCompany()));
        return ok;
    }

    @Override
    public String changeCompanyOrder(String companyCode, String type) {
        List<CompanyPojo> companyPojos = companyDao.querySamleLevelCompany(companyCode);//查询同级公司
        if (companyPojos.size()<=0||companyPojos==null) return NR.r(CodeDict.UPDATE_FAILE_CASE,"当前公司不存在");
        Integer max=Integer.MIN_VALUE,min=Integer.MAX_VALUE;
        CompanyPojo node=new CompanyPojo();
        HashMap<Integer, CompanyPojo> map = new HashMap<>();// order 和 company 的对应关系
        List<Integer> order = new LinkedList<>();
        for (CompanyPojo companyPojo : companyPojos) {//最大order order序列 当前companyPojo
            if (companyPojo.getOrder()>max){//排序最大值(最下层)   排序规则是升序
                max=companyPojo.getOrder();
            }
            if (companyPojo.getOrder()<min){//排序最小值(最上层)   排序规则是升序
                min=companyPojo.getOrder();
            }
            if (companyPojo.getCompanyCode().equals(companyCode)){
                node=companyPojo;//当前修改的company
            }
            order.add(companyPojo.getOrder());
            map.put(companyPojo.getOrder(),companyPojo);
        }
        Collections.sort(order);//对order升序排序
        if("up".equals(type)){//提升
            if (node.getOrder()<=min){
                return NR.r("最顶层节点不能上移！");
            }else {
//                for (CompanyPojo companyPojo : companyPojos) {
//                    if (companyPojo.getOrder().equals(node.getOrder()-1)){
//                        swapNode=companyPojo;
//                        break;
//                    }
//                }
                Integer swapOrder = order.get(order.indexOf(node.getOrder()) - 1);//获得排序在修改节点上一个节点的order值
                CompanyPojo swapNode = map.get(swapOrder);
                companyDao.updateCompanyOrder(swapNode.getOrder(),node.getCompanyCode());
                companyDao.updateCompanyOrder(node.getOrder(),swapNode.getCompanyCode());
            }
        }else {//下降
            if(node.getOrder()>=max){
                return NR.r("最底层节点不能下移！");
            }else{
//                for (CompanyPojo companyPojo : companyPojos) {
//                    if (companyPojo.getOrder().equals(node.getOrder()+1)){
//                        swapNode=companyPojo;
//                        break;
//                    }
//                }
                Integer swapOrder = order.get(order.indexOf(node.getOrder()) + 1);//获得排序在修改节点下一个节点的order值
                CompanyPojo swapNode = map.get(swapOrder);
                companyDao.updateCompanyOrder(swapNode.getOrder(),node.getCompanyCode());
                companyDao.updateCompanyOrder(node.getOrder(),swapNode.getCompanyCode());
            }
        }
        return NR.r();
    }

    @Override
    public String getCompanyOutDto(String sonName) {
        int max = companyDao.queryMaxLenth();
        List<CompanyOutDto> listCompanyOutSonDto = new ArrayList<>();
        List<CompanyOutDto> listCompanyOutSonDto1 = companyDao.getListCompanyOutSonDto(max, null);
        for (CompanyOutDto companyOutDto : listCompanyOutSonDto1) {
            String parentCode = companyOutDto.getSonCode().substring(0, 4);
            String parentName = companyDao.getCompanyOutDto(parentCode);
            companyOutDto.setParentCode(parentCode);
            companyOutDto.setParentName(parentName);
            listCompanyOutSonDto.add(companyOutDto);
        }
        return NR.r(listCompanyOutSonDto);
    }
}

